/*
Mif.Menu.Item
*/

Mif.Menu.Item=new Class({

	Implements: [Events],

	initialize: function(structure, options){
		$extend(this, structure);
		this.type=options.type||this.owner.dfltType;
		if($type(options)=='string'){
			this.type='separator';
		}
		this.property=options.property||{};
		this.data=options.data;
		//this.state=$extend($unlink(this.owner.dfltState), options.state);
		this.$calculate();
		this.UID=Mif.Menu.Item.UID++;
		Mif.Menu.Items[this.UID]=this;
		var id=this.property.id;
		if(id!=null) Mif.ids[id]=this;
		this.owner.fireEvent('itemCreate', [this]);
		if(this.checkbox){
			this.initCheckbox();
		}
		if(this.radio){
			this.initRadio();
		}
	},
	
	$calculate: function(){
		console.log('type', this.type);
		$extend(this, $unlink(this.owner.owner.defaults));
		this.type=$splat(this.type);
		this.type.each(function(type){
			var props=this.owner.owner.types[type];
			console.log(props);
			if(props) $extend(this, props);
		}, this);
		$extend(this, this.property);
		console.log(this.mode);
	},
	
	draw: function(){
		this.update();
	},
	
	set: function(props){
		$extend(this, props);
		this.draw();
	},
	
	disable: function(){
		if(this.disabled) return;
		this.getElement('item').addClass('mif-menu-item-disabled');
		return this;
	},
	
	enable: function(){
		if(!this.disabled) return;
		this.getElement('item').removeClass('mif-menu-item-disabled');
		return this;
	},
	
	initSubMenu: function(){
		this.options.subMenu.zIndex=this.owner.zIndex+1;
		this.subMenu=new Mif.Menu.List(this.options.subMenu, {ancestor: this, owner: this.owner.owner});
	},
	
	select: function(){	
		if(this.selected) return;
		this.selected=true;
		this.draw();
		this.owner.fireEvent('select', this);
		return this.fireEvent('select');
	},
	
	unselect: function(){
		if(!this.selected) return;
		this.selected=false;
		this.draw();
		this.owner.fireEvent('unSelect', this);
		return this.fireEvent('unSelect');
	},
	
	check: function(state){
		if(this.type=='checkbox'){
			if($defined(state)){
				if(this.checked==state) return this;
				this.checked=state;
			}else{
				this.checked=!this.checked;
			}
			if(this.checked){
				this.icon='mif-menu-checked';
			}else{
				this.icon='mif-menu-unchecked';
			}
			this.draw();
			this.owner.fireEvent('check', [this, this.checked]);
		}
		if(this.type=='radio'){
			var checked=$defined(state) ? state : true;
			if(this.checked==checked) return this;
			this.checked=checked;
			this.owner.groups[this.group].each(function(item){
				if(item==this && this.checked){
					item.checked=true;
					item.icon='mif-menu-radio-checked';
					item.owner.fireEvent('radioCheck', [this, true]);
					item.draw();
					return;
				}else{
					item.checked=false;
					item.icon='mif-menu-radio-unchecked';
					item.draw();
					item.owner.fireEvent('radioCheck', [this, false]);
				}
			}, this);
		}
		return this;
	},
	
	initCheckbox: function(){
		this.mode='checkbox';
		this.icon='mif-menu-'+(this.checked ? 'checked' : 'unchecked');
		this.addEvent('action',this.check.bind(this));
	},
	
	initRadio: function(){
		this.mode='radio';
		this.owner.groups[this.group]=(this.owner.groups[this.group]||[]).include(this);
		this.icon='mif-menu-radio-' + (this.checked ? 'checked' : 'unchecked');
		this.addEvent('action',this.check.bind(this));
	}
	
});


Mif.Menu.Item.UID=0;
Mif.Menu.Items={};